package business_example_service

import (
	"runtime/debug"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/service"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
)

func (cls *Example) UpdateExample(ctx context.Context, example *model.BusinessExample, fields ...service.BusinessExampleField) errors.Error {
	if nil == example {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISE_LE.E_LE.UE_LE.152901 example is nil stack %s", string(debug.Stack()))

		return errors.ErrorServerInternalError("ISE_LE.E_LE.UE_LE.173221", "异常的传入参数")
	}

	for _, field := range fields {
		if err := field(example); nil != err {
			return err.Relation(errors.ErrorServerInternalError("ISE_LE.E_LE.UE_LE.222814", "应用选项失败"))
		}
	}

	if err := cls.example.SaveBusinessExample(ctx, example); nil != err {
		return err.Relation(errors.ErrorServerInternalError("ISE_LE.E_LE.UE_LE.263227", "更新数据失败"))
	}

	return nil
}
